package com.zhiyu.controller;

import com.github.pagehelper.PageInfo;
import com.zhiyu.common.ApiReturn;
import com.zhiyu.pojo.*;
import com.zhiyu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

@RequestMapping("user")
@Controller
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private AddressService addressService;

    //用户登录
    @RequestMapping("login")
    @ResponseBody
    public ApiReturn login(User user, HttpServletRequest request, HttpServletResponse response){
        String result = userService.login(user);
        if (result.equals("用户不存在")){
            return ApiReturn.error("账号或者密码错误！");
        }
        //携带用户信息  result为： id:用户名
        request.getSession().setAttribute("userInfo",result);
        return ApiReturn.success("返回用户登录数据",result);
    }

    //用户登录信息
    @RequestMapping("getUserInfo")
    @ResponseBody
    public ApiReturn getUserInfo(HttpServletRequest request, HttpServletResponse response){
        //获取session中的用户信息
        String userInfo = (String) request.getSession().getAttribute("userInfo");
        if(userInfo == null || userInfo == ""){
            return ApiReturn.error("未登录");
        }
        String[] userStr = userInfo.split(":");
        User user = new User();
        user.setUserId(Integer.valueOf(userStr[0]));
        user.setUserName(userStr[1]);
        return ApiReturn.success("返回用户登录数据",user);
    }

    //用户退出登录
    @RequestMapping("logout")
    @ResponseBody
    public ApiReturn logout(HttpServletRequest request,HttpServletResponse response){
        //退出
        HttpSession httpSession = request.getSession();
        httpSession.removeAttribute("userInfo");
        Cookie[] cookies = request.getCookies();
        if(cookies != null && cookies.length >0){
            for(Cookie c : cookies){
                if("autoLogin".equals(c.getName())){
                    //设置cookie存活时间为0
                    c.setMaxAge(0);
                    //将cookie响应到前台
                    response.addCookie(c);
                    break;
                }
            }
        }else{
            return ApiReturn.error("退出失败");
        }
        return ApiReturn.success("退出成功");
    }

    //用户管理
    @RequestMapping("userManage")
    @ResponseBody
    public ApiReturn userManage(User user){
        PageInfo<User> page = userService.findPage(user);
        if (page.getList()!=null){
            return ApiReturn.success("返回用户数据",page);
        }
        return ApiReturn.error("无数据");
    }

    //用户注册
    @RequestMapping("register")
    @ResponseBody
    public ApiReturn register(User user){
        int count = userService.insert(user);
        if (count<1){
            return ApiReturn.error("注册失败");
        }
        return ApiReturn.success("注册成功");
    }

    //获得个人信息
    @RequestMapping("getUser")
    @ResponseBody
    public ApiReturn getUser(HttpServletRequest request,HttpServletResponse response){
        String userInfo = (String) request.getSession().getAttribute("userInfo");
        if (userInfo==null||userInfo.equals("")){
            return ApiReturn.error("未登录");
        }
        String[] userStr = userInfo.split(":");
        User user = userService.get(Integer.valueOf(userStr[0]));
        Address address = new Address();
        address.setUserId(Integer.valueOf(userStr[0]));
        user.setAddressList(addressService.findList(address));
        return ApiReturn.success("返回用户数据",user);
    }

    //修改用户信息
    @RequestMapping("updateUser")
    @ResponseBody
    public ApiReturn updateUser(User user,HttpServletRequest request,HttpServletResponse response){
        String userInfo = (String) request.getSession().getAttribute("userInfo");
        if (userInfo==null||userInfo.equals("")){
            return ApiReturn.error("未登录");
        }
        String[] userStr = userInfo.split(":");
        user.setUserId(Integer.valueOf(userStr[0]));
        if (user.getAddress()!=null){
            addressService.updateDefaultStatus(user.getUserId());
            addressService.updateStatus(Integer.valueOf(user.getAddress()));
        }
        int count = userService.update(user);
        if (count<1){
            return ApiReturn.error("修改失败");
        }
        return ApiReturn.success("修改成功");
    }

}
